import router from '@/router';
import { Dialog } from 'mand-mobile';
import Utils from '@/common/pakjUtils';
import Loading from '@/components/loading';
import WxUtils from '@/common/weixinUtils';
let isShowing = false;
export default {
  show(msg, from = undefined, to = undefined) {
    if (isShowing) {
      return;
    }
    Loading.disable();
    Loading.closeAll();
    isShowing = true;
    Dialog.alert({
      title: '错误',
      content: msg,
      confirmText: '去登录',
      onConfirm: () => {
        if (Utils.isApp()) {
          // 判断当前环境是否在app
          /* eslint-disable */
          var os = Utils.getOSName();
          if ((os === 'Linux' || os === 'Android') && handler) {
            handler.pageOut();
          } else if ((os === 'iOS' || os === 'MacOSX') && window.webkit) {
            window.webkit.messageHandlers.pageOut.postMessage(
              JSON.stringify('')
            );
          }
        } else if (Utils.getCurrentEnv() === 'wx') {
          if (window.location.host.indexOf('h5') >= 0) {
            if (router.currentRoute.meta.continue) {
              let WxAuthRouteInfo = {
                routeName: router.currentRoute.name,
                continue: router.currentRoute.meta.continue,
                query: router.currentRoute.query
              };
              let json = JSON.stringify(WxAuthRouteInfo);
              localStorage.setItem('WxAuthRouteInfo', json);
            }
            WxUtils.startWxAuth();
          } else {
            localStorage.clear();
            sessionStorage.clear();
            if (window.wx) {
              window.wx.closeWindow();
            }
            setTimeout(() => {
              router.push({
                name: 'result'
              });
            }, 1000);
          }
        } else if (Utils.getCurrentEnv() === 'mini') {
          WxUtils.restartMiniProgram();
        } else {
          Loading.enable();
          isShowing = false;
          sessionStorage.clear();
          let con = router.currentRoute.meta.continue;
          let current = router.currentRoute.name;
          let obj = router.currentRoute.query;
          if (to) {
            con = to.meta.continue;
            current = to.name;
            obj = to.query;
          }
          if (con) {
            obj.next = current;
          }
          obj.action = 'reg';
          router.push({
            name: 'Login',
            query: obj
          });
        }
      }
    });
  }
};
